<template>
	<div class="dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" :close-on-click-modal="false" width="500px">
			<el-form ref="formRef" :model="state.ruleForm" :rules="state.rules">
				<el-row class="mb20" :gutter="20">
					<el-col :span="24">
						<el-form-item label="当前推荐人" prop="invite_name">
							<el-input v-model="state.ruleForm.invite_name" disabled />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row class="mb20" :gutter="20">
					<el-col :span="24">
						<el-form-item label="新推荐人" prop="invite_id">
							<el-select
								v-model="state.ruleForm.invite_id"
								clearable
								placeholder="请输入手机号/选择推荐人"
								filterable
								remote
								:remote-method="inviteIdRemoteMethod"
								remote-show-suffix
								:loading="state.inviteIdSelect.loading"
								@focus="onInviteIdFocus"
							>
								<el-option v-for="item in state.inviteIdSelect.options" :key="item.id" :label="item.nickname" :value="item.id" />
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<el-button @click="onCancel" size="default">取 消</el-button>
				<el-button type="primary" @click="onSubmit" :loading="state.dialog.submitLoading" size="default">提 交</el-button>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" name="templateDialog" setup>
import { ElMessage } from 'element-plus';
import { reactive, ref } from 'vue';
import { usersApi } from '/@/api';

let callback: Function = () => {};
const UsersApi = usersApi();
const state = reactive({
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitLoading: false,
	},
	ruleForm: {
		user_id: '',
		invite_id: '',
		invite_name: '',
	},
	rules: {
		invite_id: [{ required: true, message: '新推荐人不能为空', trigger: ['blur', 'change'] }],
	},
	inviteIdSelect: {
		loading: false,
		options: [] as any,
	},
});
const formRef = ref();

/********** 触发事件 **********/
// 新推荐人获取焦点
const onInviteIdFocus = () => {
	if (state.ruleForm.invite_id) return;
	_usersInviteList({ pageNum: 1, pageSize: 10 });
};
// 取消
const onCancel = () => {
	state.dialog = {
		isShowDialog: false,
		type: '',
		title: '',
		submitLoading: false,
	};
};
// 提交
const onSubmit = () => {
	formRef.value.validate((valid: boolean) => {
		if (!valid) return;
		_usersEditInvite(state.ruleForm);
	});
};

/********** 方法事件 **********/
// 打开
const openDialog = (row: any, fn: Function) => {
	state.dialog = {
		isShowDialog: true,
		type: '',
		title: `「${row.nickname}」修改推荐人`,
		submitLoading: false,
	};
	callback = fn;
	state.ruleForm = {
		invite_name: row.invite_name,
		user_id: row.id,
		invite_id: '',
	};
};
// 新推荐人搜索选择
const inviteIdRemoteMethod = (query: string) => {
	if (!query) return;
	_usersInviteList({ pageNum: 1, pageSize: 10, mobile: query });
};

/********** 网络请求 **********/
// 可以选择成为推荐人的用户列表
const _usersInviteList = (params: Object) => {
	state.inviteIdSelect.loading = false;
	UsersApi.usersInviteList(params)
		.then((res) => {
			state.inviteIdSelect.options = res.data.list;
		})
		.catch(() => (state.inviteIdSelect.loading = false));
};
// 修改推荐人
const _usersEditInvite = (params: Object) => {
	UsersApi.usersEditInvite(params)
		.then(() => {
			ElMessage.success('操作成功！');
			onCancel();
			callback();
		})
		.catch(() => (state.dialog.submitLoading = false));
};

/********** 方法向父组件暴露 **********/
defineExpose({ openDialog });
</script>
